package com.crowdfunding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.crowdfunding.entity.RecordCrowdFunding;
import com.crowdfunding.mapper.RecordCrowdFundingMapper;
import com.crowdfunding.service.IRecordCrowdFundingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoyu
 * @since 2021-07-14
 */
@Service
public class RecordCrowdFundingServiceImpl extends ServiceImpl<RecordCrowdFundingMapper, RecordCrowdFunding> implements IRecordCrowdFundingService {

    @Autowired
    RecordCrowdFundingMapper recordCrowdFundingMapper;

    @Override
    public void addRecord(RecordCrowdFunding crowdFundin) {
        recordCrowdFundingMapper.insert(crowdFundin);
    }

    @Override
    public List<RecordCrowdFunding> getRecordList(String cfId){
        List<RecordCrowdFunding> cfIdList = recordCrowdFundingMapper.selectList(new QueryWrapper<RecordCrowdFunding>().eq("cf_id", cfId));
        return cfIdList;
    }

    public List<RecordCrowdFunding> getRecordListByUsername(String username){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("username",username);
        List<RecordCrowdFunding> list = recordCrowdFundingMapper.selectList(wrapper);
        return list;
    }

    @Override
    public double getRecordTotalAmount(String username, String cfId){
        double totalAmount=0;
        List<RecordCrowdFunding> list=this.getRecordListByUsername(username);
        for (RecordCrowdFunding record: list
             ) {
            totalAmount+=record.getCfrAmount();
        }
        return totalAmount;
    }
}
